/**
 * 类型守卫
 */

// 将类型,限制在string
// ! 自定义类型守卫->形参 is A类型 = boolean + 类型守卫能力
function isString(str: any): str is string {
  return typeof str === "string";
}

function fun(str: any) {
  if (isString(str)) {
    console.log("我是string", str, str.length);
    // 类型守卫之后,可以使用string的提示方法
  }
}

fun("wwww");

// ! 不允许修改参数

function showArr(arr: readonly any[]) {
  // arr[1] = 'aaaa'
  console.log("arr", arr);
}

showArr(["111", 2, "3"]);

// ! 可变元祖
// 如果遇到name声明已经声明过的报错,可以export {};会被是为module 不会相互影响
// name:string,age:number,...rest:any[] 添加标签,增加可读性
let [name, no, ...rest]: [name: string, age: number, ...rest: any[]] = [
  "汪苏泷",
  19,
  "我是大帅哥",
  "我是大美女",
];

console.log("name", name, "no", no, "rest", rest);

// 如果用as const 会报错 The type 'readonly ["11", 112, 444, 55]' is 'readonly' and cannot be assigned to the mutable type '(string | number)[]'
const arr: (string | number)[] = ["11", 112, 444, 55];
// arr[0] = '222';

console.log("arr", arr);

// 可变元祖跟定义好的元祖,不兼容
const changeArr1 = [10, 11, 10, "wsl"];
// const [a1,...a2]:[number,...any[]] = changeArr1//!报错
// const [a1, ...a2]: [...any[]] = changeArr1;// 把元祖退化成了'数组' 

// ! 想要固定住数组 不修改的话,把类型设置为可读
const changeArr = [10, 11, 10, "wsl"]  as const;
const [a1,...a2]:readonly[any,...any[]] = changeArr
console.log("可变元祖的转换", a1, a2);


// readonly等效于as const
function tail(cust5:readonly (string | number)[]){
  let temp = cust5;
  // cust5[0] = 1 //!报错
  let [first,...rest] = cust5
  console.log('tail返回',rest);
}
tail(changeArr1)
